#-*-coding:utf-8-*-
import os
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class getAllElementAtt():
    '''根据您传入的读取文件名、和目标文件名
    将元素远洋存在指定文件中'''
    def getAllElementAtt(self,sourcename,targetName):
        sourcename = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "..")) + '//FunctionClass//' + sourcename + '.html'
        soup=BeautifulSoup(open(sourcename),from_encoding = 'utf-8')
        attsFilePath = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + '//Resources//'+targetName+'.py'
        isContuineWrite=True
        if (os.path.exists(attsFilePath)):
            print("==========================准备开始了==================")
            isContuineStr = raw_input("文件已经存在是否重写？Y/N")
            if isContuineStr != "Y":
                isContuineWrite = False
            else:
                os.remove(attsFilePath)
        if isContuineWrite:
            f = open(attsFilePath, 'a+')
            f.write(
                "# encoding=utf-8" + "\n" + "import sys,os" + "\n" + "reload(sys)" + "\n" + "sys.setdefaultencoding('utf-8')" + "\n")
            isContinue=True
            for child in soup.descendants:
                print("=======打印子标签=======")
                print(child)
                des = raw_input("请输入注释信息: ")
                if  des=='end':
                    isContinue=False
                    break
                elif des != '' and des!='end':
                    try:
                        print(child.attrs)
                        atts=child.attrs
                    except Exception as e:
                        atts = '\''+str(child)+'\''

                    name = raw_input("请输入命名: \n")
                    f.write("#" + des + "\n")
                    f.write(name + "=" + str(atts).encode('utf-8') + "\n")

                    '''
                    name = raw_input("请输入需要录入的属性，多属性以英文,分割: \n")
                    atts=name.split(',')
                    for att in atts:
                        try:
                            print(att)
                            print(child[att])
                            #f.write(name + "=\"" + id + "\"" + "\n")
                        except  Exception as e:
                            print("不存在该属性值")
                            #raise e
                    '''
    def test(self):
        '''unicode转码，个别需要用到，不在拆分原来程序了'''
        content= u'\u5b66\u6821\u7ba1\u7406\uff0c\u5bb6\u56ed\u5171\u80b2\uff0c\u5b66\u6821APP\uff0c\u5bb6\u56ed\u5171\u80b2\uff0c\u5bb6\u56ed\u4e92\u52a8\uff0c\u5b66\u6821\u53d1\u77ed\u4fe1\uff0c\u5b66\u6821\u8f6f\u4ef6\uff0c\u5bb6\u6821\u6c9f\u901a\uff0c\u667a\u6167\u5e7c\u6559\uff0c\u667a\u6167\u6811\uff0c\u8d1d\u804a\uff0c\u638c\u901a\u5bb6\u56ed\uff0c\u5b9d\u8d1d+\uff0c\u4f18\u84d3\u901a\uff0c\u5fae\u5bb6\u56ed\uff0c\u6821\u8baf\u901a'
        print(str(content).encode('utf-8'))

getAllElementAtt=getAllElementAtt()
getAllElementAtt.getAllElementAtt("Welcome","WelcomeAtt")